﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using NSDecisionTree.Tools;

namespace NSDecisionTree.Gui
{
    public partial class ShowNodeRepresentation : Form
    {
        Graphic.Node nodeToRepresent = null;

        internal Graphic.Node NodeToRepresent
        {
            get { return nodeToRepresent; }
            set { nodeToRepresent = value; }
        }

        public ShowNodeRepresentation()
        {
            InitializeComponent();
        }

        private void ShowNodeRepresentation_Load(object sender, EventArgs e)
        {
            if (nodeToRepresent != null)
            {
                try
                {
                    DataTable dt = createDataTable(nodeToRepresent);
                    dataGridView1.DataSource = dt;
                    dataGridView1.Columns[nodeToRepresent.Name].DefaultCellStyle.BackColor = Color.LightGreen;
                    dataGridView1.ReadOnly = true;
                    dataGridView1.AllowUserToAddRows = false;
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Es ist ein Fehler aufgetreten." + System.Environment.NewLine + ex.Message);
                }
                
            }
        }

        [Obsolete]
        private void drawTable()
        {
            Graphic.Node parent = nodeToRepresent.Parent;
            Graphic.Node current = nodeToRepresent;
            List<string> nodeNames = new List<string>();
            List<string> rowValues = new List<string>();
            nodeNames.Add(nodeToRepresent.Name);

            while (parent != null)
            {
                nodeNames.Insert(0,parent.Name);
                rowValues.Insert(0, current.EdgeToParent.Description);
                current = parent;
                parent = parent.Parent;
            }

            string rowValueTilNow = String.Empty;

            foreach (String val in rowValues)
            {
                rowValueTilNow += val + "; ";
            }

            List<string> resultRows = new List<string>();

            foreach (Graphic.Node child in nodeToRepresent.Children)
            {
                if (! child.IsThisNodeLeaf)
                {
                    while (true)
                    {
                        nodeNames.Add(child.Name);
                        
                        if (child.Children.Count == 0)
                            break;
                    }
                }
                else
                {
                    resultRows.Add(rowValueTilNow + child.EdgeToParent + "; " + child.Name);
                }
            }

            nodeNames.Add("Result");

            DataTable dt = new DataTable();
            foreach (string element in nodeNames)
            {
                DataColumn col = new DataColumn(element);
                dt.Columns.Add(col);
            }

            foreach (string element in resultRows)
            {
                dt.Rows.Add(element.ToSeperatedString(";"));
            }

            dataGridView1.DataSource = dt;
        }

        private DataTable createDataTable(Graphic.Node currentRootNode)
        {
            List<string> columns = new List<string>();
            List<string> rows = new List<string>();
            columns.Add(currentRootNode.Name);
            int itemCounter = 0;

            Graphic.Node parent = currentRootNode.Parent;
            Graphic.Node current = currentRootNode;
            string currentRowString = String.Empty;
            int itemsFilled = 0;
            while (parent != null)
            {
                columns.Insert(0, parent.Name);
                currentRowString = current.EdgeToParent + ";" + currentRowString;
                current = parent;
                parent = parent.Parent;
                itemsFilled++;
             }

            foreach (Graphic.Node childNode in currentRootNode.Children)
            {
                fillList(childNode, ref columns, ref itemCounter);
            }
            columns.Add("Result");
            columns.Add("Count");

            fillRowList(currentRootNode, ref rows, columns, itemsFilled, currentRowString);

            DataTable dt = new DataTable();
            foreach (string column in columns)
            {
                dt.Columns.Add(new DataColumn(column));
            }
            foreach (string row in rows)
            {
                dt.Rows.Add(row.ToSeperatedString(";"));
            }

            return dt;
        }

        private void fillList(Graphic.Node node, ref List<string> columnsList, ref int itemCounter)
        {
            if (node.IsThisNodeLeaf)
            {
                itemCounter++;
                return;
            }
            else
            {
                if (! columnsList.Contains(node.Name))
                    columnsList.Add(node.Name);
            }

            if (node.Children.Count != 0)
            {
                foreach (Graphic.Node child in node.Children)
                    fillList(child, ref columnsList, ref itemCounter);
            }
        }

        private void fillRowList(Graphic.Node rootNode, ref List<string> rowList, List<string> columns, int itemsFilled, string currentRowString)
        {
            foreach (Graphic.Node child in rootNode.Children)
            {
                fillRowListItems(child, ref columns, itemsFilled, currentRowString, ref rowList);
            }

        }

        private void fillRowListItems(Graphic.Node child, ref List<string> columns_2, int columnCounter, String currentRowString, ref List<string> rowList)
        {
            if (child.Parent.Name == columns_2[columnCounter])
            {
                currentRowString += child.EdgeToParent + "; ";
                // fillRowListItems(child, ref columns_2, columnCounter + 1, currentRowString, ref rowList);
            }
            
            if (child.IsThisNodeLeaf)
            {
                if (columns_2[columnCounter + 1] == "Result")
                {
                    currentRowString += child.Name + "; " + child.SumChildCount.ToString();
                    rowList.Add(currentRowString);
                }
                else
                {
                    currentRowString += " ; ";
                    fillRowListItems(child, ref columns_2, columnCounter + 1, currentRowString, ref rowList);
                }
            }

            foreach (Graphic.Node children in child.Children)
            {
                fillRowListItems(children, ref columns_2, columnCounter + 1, currentRowString, ref rowList);
            }

        }
        
    }
}
